package khleuven.mondzorg.domain;

import java.util.Arrays;

import khleuven.mondzorg.Logger;

import org.json.JSONObject;

public class Test extends ModuleBasedEntry {
	
	private String question;
	private String[] answers;	

	@Deprecated
	public Test(Language language, DestinationGroup destination, 
			Module module, int seq, String question, String[] answers)
			throws DomainException {
		super(EntryType.TEST, language, destination, module, seq);
		setQuestion(question);
		setAnswers(answers);
	}

	public Test(JSONObject object) throws DomainException {
		super(EntryType.TEST, object);
		setQuestion(JsonKeysDictionary.getStringItem(object, JsonKeysDictionary.JSON_KEY_TEST_QUESTION));
		setAnswers(JsonKeysDictionary.getStringItem(object, JsonKeysDictionary.JSON_KEY_TEST_ANSWERS));
		Logger.logDomain(toString());
	}

	public String getQuestion() {
		return question;
	}

	private void setQuestion(String question) {
		this.question = question;
	}

	public String[] getAnswers() {
		return answers;
	}
	
	private void setAnswers(String[] answers) {
		this.answers = answers;
	}
	
	public String getAnswersAsString() {
		return ""+getAnswers()[0]+"; "+getAnswers()[1]+";"+getAnswers()[2];
	}
	
	private void setAnswers(String answers) {
		this.answers = answers.split(";");
	}
	
	@Override
	public String makeStringForList() {
		return getQuestion();
	}
	
	@Override
	public String toString(){
		return super.toString()+getQuestion()+": "+getAnswers()[0]+"; "+getAnswers()[1]+";"+getAnswers()[2];
	}
	
	@Override
	public JSONObject toJSon() throws DomainException {
		JSONObject output = super.toJSon();
		JsonKeysDictionary.toStringItem(output, JsonKeysDictionary.JSON_KEY_TEST_QUESTION, getQuestion());
		JsonKeysDictionary.toStringItem(output, JsonKeysDictionary.JSON_KEY_TEST_ANSWERS, getAnswersAsString());
		return output;		
	}
	
	@Override
	public boolean equals(Object obj) {
		boolean gelijk = false;
		if(obj instanceof Test){
			Test t = (Test)(obj);
			if(super.equals(t) && 
				t.getQuestion().equals(this.getQuestion()) &&
				t.getAnswers()[0].equals(this.getAnswers()[0]) &&
				t.getAnswers()[1].equals(this.getAnswers()[1]) &&
				t.getAnswers()[2].equals(this.getAnswers()[2]) ){
				gelijk = true;
			}
		}		
		return gelijk;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + Arrays.hashCode(answers);
		result = prime * result	+ ((question == null) ? 0 : question.hashCode());
		return result;
	}

	@Override
	public int compareTo(Entry t) {
		int r = super.compareTo(t);
		if(r == 0) {
			Test o = (Test)t; 
			// CCE not possible because of super.compareTo(t)
			return this.getQuestion().compareTo(o.getQuestion());
		} else {
			return r;
		}
	}
}